package dog.panhu.keyworddemo.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import dog.panhu.keyworddemo.R;
import dog.panhu.keyworddemo.dialog.PanhuBottomSheetDialog;
import jackmego.com.jieba_android.JiebaSegmenter;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button analysisBt;
    private EditText contentEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initData() {

    }

    private void initView() {
        analysisBt = findViewById(R.id.analysisBt);
        analysisBt.setOnClickListener(this);

        contentEt = findViewById(R.id.contentEt);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.analysisBt:
                Toast.makeText(this, "点击了按钮", Toast.LENGTH_SHORT).show();

                String text = contentEt.getText().toString().trim();
                analysis(text);
                break;
        }
    }

    /**
     * 分词
     *
     * @param text
     */
    private void analysis(String text) {
        if (TextUtils.isEmpty(text)) {
            Toast.makeText(this, "请输入内容", Toast.LENGTH_SHORT).show();
            return;
        }
        //获取工具类管理对象
        JiebaSegmenter jiebaSegmenter = jackmego.com.jieba_android.JiebaSegmenter.getJiebaSegmenterSingleton();
        //判断是否初始化
        if (null == jiebaSegmenter) {
            Toast.makeText(this, "请先在Application里面初始化JiebaSegmenter", Toast.LENGTH_SHORT).show();
            return;
        }
        //进行分词
        ArrayList<String> wordList = jiebaSegmenter.getDividedString(text);

        PanhuBottomSheetDialog panhuBottomSheetDialog = new PanhuBottomSheetDialog(MainActivity.this, wordList);
        panhuBottomSheetDialog.show();

    }
}
